﻿using AutoMapper;
using Infrastructure.Extend;
using Infrastructure.Web;
using MyData;
using MyData.Dto;
using MyData.Models;
using MyDomain;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyApplication
{
    public class DepartMobileRLApp
    {
        private IDepartMobileRLRepository _service;
        private IMobileRepository _mobileService;
        private ISyncRLRepository _syncRLService;
        private IUserInfoRepository _userInfoService;
        public DepartMobileRLApp(IDepartMobileRLRepository Service,
            IMobileRepository mobileService,
            ISyncRLRepository syncRLService,
            IUserInfoRepository userInfoService)
        {
            _service = Service;
            _mobileService = mobileService;
            _syncRLService = syncRLService;
            _userInfoService = userInfoService;
        }
        public ConfigMobileDto GetConfigMobileDto(Guid departId)
        {
            ConfigMobileDto configMobileDto = new ConfigMobileDto();
            configMobileDto.Id = departId;
            var mobiles = _mobileService.IQueryable().ToList();
            var result = _service.IQueryable().Where(c => c.DepartId == departId).Select(c => c.MobileId);
            foreach (var item in mobiles)
            {
                ConfigMobileChild configMobileChild = new ConfigMobileChild();
                configMobileChild.Id = item.Id;
                if (result.Contains(item.Id))
                {
                    configMobileChild.IsSelected = true;
                }
                configMobileChild.Name = item.Name;
                configMobileDto.ConfigMobiles.Add(configMobileChild);
            }
            return configMobileDto;

        }
        public bool SaveConfigMobile(Guid departId, List<Guid> listMobiles)
        {
            var oldMobiles = _service.IQueryable().Where(c => c.DepartId == departId).Select(c => c.MobileId).ToList();
            if (listMobiles == null) listMobiles = new List<Guid>();
            var deleMobiles = oldMobiles.Where(c => !listMobiles.Contains(c)).ToList();
            foreach (var item in deleMobiles)
            {
                AddSyncRLs(departId, item, (int)EnumOperateType.删除);
            }

            _service.Delete(c => c.DepartId == departId && !listMobiles.Contains(c.MobileId));
            List<DepartMobileRL> listModels = new List<DepartMobileRL>();
            if (listMobiles != null)
            {
                foreach (var item in listMobiles)
                {
                    if (oldMobiles.Contains(item))
                        continue;
                    AddSyncRLs(departId, item, (int)EnumOperateType.新增);
                    listModels.Add(new DepartMobileRL()
                    {
                        Id = Guid.NewGuid(),
                        DepartId = departId,
                        MobileId = item
                    });
                }
                return _service.Insert(listModels) > 0;
            }
            return true;
        }
        private void AddSyncRLs(Guid departId, Guid mobileId, int operate)
        {
            List<SyncRL> syncRLList = new List<SyncRL>();
            var userList = _userInfoService.IQueryable().Where(c => c.DepartId == departId&&c.NeedConfirm==0&&!string.IsNullOrEmpty(c.HeadUrl));
            foreach (var item in userList)
            {
                _syncRLService.Delete(c => c.MobileId == mobileId && c.UserId == item.Id);
                syncRLList.Add(new SyncRL()
                {
                    Id = Guid.NewGuid(),
                    MobileId = mobileId,
                    OperateType = operate,
                    UserId = item.Id,
                    UserType=(int)EnumUserType.员工
                });
            }
            _syncRLService.Insert(syncRLList);
        }

    }
}
